#ifndef _MR88FX02_BSP_H_
#define _MR88FX02_BSP_H_

#include "MR88FX02.h"

#define STATIC static
#define EXTERN extern
#define CONST const
#define VOLATILE volatile
#define NOP()	__nop()

#define DISEN			0
#define ENABLE		1

#define FALSE		0
#define TRUE		1

#define E_NOK		0
#define E_OK		1
#define ERROR		2

#define	HardFault_Reset_MCU	1

#define MR88FX02_PORT_TAB_LEN			16+16+13+7		//52 
#define MR88FX02_ADC_TAB_LEN			27
//-----------------------------------------------------------
//	 interrupt open or close
//-----------------------------------------------------------
EXTERN void MR_Disable_Interrupt(void);
EXTERN void MR_Enable_Interrupt(void);

//-----------------------------------------------------------
//   define enable watch dog function
//-----------------------------------------------------------  
#define	WDT_128MS	0X0
#define	WDT_256MS 0X1
#define	WDT_512MS	0X2
#define	WDT_1S		0X3
#define	WDT_2S		0X4
#define WDT_4S		0X5
#define WDT_8S		0X6
#define WDT_16S		0X7

#define MR_WDT_ENABLE	1		//for WDT MACRO definition

EXTERN void MR_Wdg_Feed(void);
EXTERN void MR_WDT_INIT(uint8_t WDT_Period);	//WDT_Period can select from WDT_128MS to WDT_16S
//-----------------------------------------------------------
//   define low voltage reset function
//----------------------------------------------------------- 
//0x00000:1.8V
#define REF_SVD_VOLT	18

void SVD_Init(uint8_t value);
//-----------------------------------------------------------
//   define system clock mode
//-----------------------------------------------------------  
//extern typedef enum 
//{
//    CoreClk_HSRC8M,
//    CoreClk_HSRC16M,
//    CoreClk_HSRC24M,
//    CoreClk_HSRC48M,
//    CoreClk_LSRC,
//    CoreClk_XTAL1L,    		// XTAL 1 32kHz
//    CoreClk_XTAL1H,    		// XTAL 1 High Speed up to 24 MHz
//    CoreClk_XTAL1H_32MHz, // XTAL 1 High Speed up to 32 MHz with 1 WS
//    CoreClk_XTAL2,      	// XTAL 2 High Speed up to 24 MHz
//    CoreClk_XTAL2_32MHz   // XTAL 2 High Speed up to 32 MHz with 1 WS
//} eCoreClk;

EXTERN void MR88FX02_SysClk_Init(eCoreClk mode,uint32_t Ext_Osc_Freq,uint32_t SysClk_Freq);
//=====================================PORT INFO======================================================
//
//   all port define, largest group from P0 to P15 
//  
#define PORT_A_0		0
#define PORT_A_1		1
#define	PORT_A_2		2
#define	PORT_A_3		3
#define	PORT_A_4		4
#define	PORT_A_5		5
#define	PORT_A_6		6
#define	PORT_A_7		7
	
#define	PORT_B_0		8
#define	PORT_B_1		9
#define	PORT_B_2		10
#define	PORT_B_3		11
#define	PORT_B_4		12
#define	PORT_B_5		13
#define	PORT_B_6		14
#define	PORT_B_7		15
	
#define	PORT_C_0		16
#define	PORT_C_1		17
#define	PORT_C_2		18
#define	PORT_C_3		19
#define	PORT_C_4		20
#define	PORT_C_5		21
#define	PORT_C_6		22
#define	PORT_C_7		23
	
#define	PORT_D_0		24
#define	PORT_D_1		25
#define	PORT_D_2		26
#define	PORT_D_3		27
#define	PORT_D_4		28
#define	PORT_D_5		29
#define	PORT_D_6		30
#define	PORT_D_7		31

#define PORT_A_8		128
#define PORT_A_9		129
#define	PORT_A_10		130
#define	PORT_A_11		131
#define	PORT_A_12		132
#define	PORT_A_13		133
#define	PORT_A_14		134
#define	PORT_A_15		135

#define PORT_B_8		136
#define PORT_B_9		137
#define	PORT_B_10		138
#define	PORT_B_11		139
#define	PORT_B_12		140
#define	PORT_B_13		141
#define	PORT_B_14		142
#define	PORT_B_15		143

#define PORT_C_8		144
#define PORT_C_9		145
#define	PORT_C_10		146
#define	PORT_C_11		147
#define	PORT_C_12		148
#define	PORT_C_13		149
#define	PORT_C_14		150
#define	PORT_C_15		151

#define PORT_D_8		152  
#define PORT_D_9		153  
#define	PORT_D_10		154  
#define	PORT_D_11		155  
#define	PORT_D_12		156  
#define	PORT_D_13		157  
#define	PORT_D_14		158  
#define	PORT_D_15		159

#define INPUT		0
#define OUTPUT	1
#define SPECIAL	2
#define ANALOG	3

#define NOUSE   0

#define	FUNC0		0
#define FUNC1		1
#define FUNC2		2
#define FUNC3		3
#define FUNC4		4
#define FUNC5		5
#define	FUNC6		6
#define FUNC7		7
#define FUNC8		8
#define FUNC9		9
#define FUNC10	10
#define FUNC11	11
#define FUNC12	12
#define FUNC13	13
#define FUNC14	14
#define FUNC15	15
//// type of element in port infomation table //////
typedef struct Port_Table_Type_st1
{      
	uint8_t Port;
	uint8_t Direct;	
	uint8_t Function;
}Port_Table_Type;
//
//   function list
//  
//EXTERN uint8_t MR88FX02_Port_Init(CONST Port_Table_Type* p_port_tab);
EXTERN uint8_t MR88FX02_Port_Init(CONST Port_Table_Type* p_port_tab);
EXTERN uint8_t MR88FX02_Gpio_Get(uint8_t name);
EXTERN uint8_t MR88FX02_Inter_Gpio_Get(uint8_t name);
EXTERN void MR88FX02_Gpio_Set(uint8_t name, uint8_t value);
EXTERN void MR88FX02_Inter_Gpio_Set(uint8_t name, uint8_t value);
EXTERN void MR88FX02_Gpio_Set_Mode(uint8_t name, uint8_t dir);
EXTERN void MR88FX02_Gpio_Set_Pull_Up(uint8_t name, uint8_t value);
EXTERN void MR88FX02_Gpio_Set_Pull_Down(uint8_t name, uint8_t value);
EXTERN void MR88FX02_Gpio_Set_Open_Drain(uint8_t name, uint8_t value);

#define FOUT_HSRC_DIV_64		0X0
#define FOUT_XTAL1_DIV_64		0X1
#define FOUT_XTAL2_DIV_64		0X2
#define FOUT_AHBCLK_DIV_64	0X3
#define FOUT_APBCLK_DIV_64 	0X4
#define FOUT_LSRC						0X5
#define	FOUT_CLK8K					0X6
#define	FOUT_ADCCLK					0X7
#define FOUT_HSRC						0X8
#define FOUT_XTAL1					0X9
#define FOUT_XTAL2					0XA
#define FOUT_HSRC_DIV_256		0XB

EXTERN void MR88FX02_PB11_Fout(uint8_t value);
//==================================ADC INFO=========================================================
#define ADC_AN0		0
#define ADC_AN1		1
#define ADC_AN2		2
#define ADC_AN3		3
#define ADC_AN4		4
#define ADC_AN5		5
#define ADC_AN6		6
#define ADC_AN7		7
#define ADC_AN8		8
#define ADC_AN9		9
#define ADC_AN10	10
#define ADC_AN11	11
#define ADC_AN12	12
#define ADC_AN13	13
#define ADC_AN14	14
#define ADC_AN15	15
#define ADC_AN16	16
#define ADC_AN17	17
#define ADC_AN18	18
#define ADC_AN19	19
#define ADC_AN20	20
#define ADC_AN21	21
#define ADC_AN22	22
#define ADC_AN23	23
#define ADC_AN24	24
#define ADC_AN25	25
#define ADC_AN26	26
//==========================================================================
typedef struct 
{
	uint8_t Channel;
	uint8_t	Port;
	uint8_t Enable;
}Adc_Table_Type;

//
//   function declare 
//  
EXTERN void MR88FX02_Adc_Start_Convert(void);
EXTERN uint8_t MR88FX02_Adc_Check_Cmplt_Flag(void);
EXTERN void MR88FX02_Adc_Clr_Cmplt_Flag(void);
EXTERN uint8_t MR88FX02_Adc_Check_Channel (void);
EXTERN void MR88FX02_Adc_Set_Convert_Channel (uint8_t channel);
EXTERN uint16_t MR88FX02_Adc_Get_Result(uint8_t x);
EXTERN uint8_t MR88FX02_Adc_Init(CONST Adc_Table_Type* p_adc_info);

//=====================================TIMER INFO====================================================

//timer channel group
#define GTIMER0_CH1	1
#define GTIMER0_CH2	2
#define GTIMER1_CH1	3
#define GTIMER1_CH2	4
#define GTIMER2_CH1	5
#define GTIMER2_CH2	6
#define BEEP0				7
#define BEEP1				8

//timer group
#define TIMER_GROUP_NUM 6
#define GTIMER0   9
#define GTIMER1   10
#define GTIMER2   11
#define BSTIMER0  12
#define BSTIMER1  13
#define WATCHTIM	14

#define INTERNAL_GROUP	0
#define PWM_GROUP				1
#define FM_GROUP				1

typedef struct Timer_Table_Type_st {
	uint8_t Port;
	uint8_t Timerx_Channel;
	uint32_t Stand_Freq;
}Timer_PWM_Table_Type;

typedef struct Timer_Table_Interval{
	uint8_t Timerx;
	uint32_t Stand_Freq;
}Timer_Interval_Table_Type;

//
//   functions list 
//  
EXTERN uint8_t GTIM0_CH1_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN void GTIM0_CH1_PWM_Start(void);
EXTERN void GTIM0_CH1_PWM_Stop(void);

EXTERN uint8_t GTIM0_CH2_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN void GTIM0_CH2_PWM_Start(void);
EXTERN void GTIM0_CH2_PWM_Stop(void);

EXTERN uint8_t GTIM0_INTERVAL_INIT(uint32_t Sysclock,uint32_t f);

EXTERN uint8_t GTIM1_CH1_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN void GTIM1_CH1_PWM_Start(void);
EXTERN void GTIM1_CH1_PWM_Stop(void);

EXTERN uint8_t GTIM1_CH2_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN void GTIM1_CH2_PWM_Start(void);
EXTERN void GTIM1_CH2_PWM_Stop(void);

EXTERN uint8_t GTIM1_INTERVAL_INIT(uint32_t Sysclock,uint32_t f);

EXTERN uint8_t GTIM2_CH1_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN void GTIM2_CH1_PWM_Start(void);
EXTERN void GTIM2_CH1_PWM_Stop(void);

EXTERN uint8_t GTIM2_CH2_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN void GTIM2_CH2_PWM_Start(void);
EXTERN void GTIM2_CH2_PWM_Stop(void);

EXTERN uint8_t GTIM2_INTERVAL_INIT(uint32_t Sysclock,uint32_t f);

EXTERN uint8_t BSTIMER0_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN uint8_t BSTIMER0_INTERVAL_INIT(uint32_t Sysclock,uint32_t f);

EXTERN uint8_t BSTIMER1_PWM_INIT(uint32_t Sysclock,uint32_t f);
EXTERN uint8_t BSTIMER1_INTERVAL_INIT(uint32_t Sysclock,uint32_t f);

EXTERN uint8_t WTIM_INTERVAL_INIT(uint32_t Sysclock,uint32_t f);

EXTERN uint8_t MR88FX02_Timer_Init(CONST Timer_PWM_Table_Type* P,CONST Timer_Interval_Table_Type* P1,uint32_t Sysclock);
EXTERN void MR88FX02_Set_Freq(uint8_t Channel,uint32_t SysClock,uint16_t Freq);
EXTERN void MR88FX02_Squar_Stop(uint8_t Channel);
EXTERN void MR88FX02_Pwm_Set(uint8_t Channel, uint16_t Duty);
EXTERN void MR88FX02_Inter_Pwm_Set(uint8_t Channel, uint16_t Duty);
//======================================UART INFO=======================================================
// type of element in uart table //
typedef struct
{
	uint8_t Port;
	uint8_t Channel;
}Uart_Table_Type;

#define UART_STOP_1BIT	0
#define UART_STOP_2BIT	1

#define UART_NO_CHECK		0
#define UART_EVEN_CHECK 1
#define UART_ODD_CHECK 	2

#define UART_DATA_7BIT	0
#define UART_DATA_8BIT	1
#define UART_DATA_9BIT	2

//uartMode
#define UART_Rx_Disable             (0)
#define UART_Rx_Enable              (1)

#define UART_Tx_Disable             (0)
#define UART_Tx_Enable		          (1)

//uart interrupt Mode
#define UART_Rx_INT_Disable          (0)
#define UART_Rx_INT_Enable           (1)

#define UART_Tx_INT_Disable          (0)
#define UART_Tx_INT_Enable        	 (1)

#define	UART0_TX	1
#define	UART0_RX	2
#define	UART1_TX	3
#define	UART1_RX	4
#define	UART2_TX	5
#define	UART2_RX	6

typedef struct uartx
{
	struct{
		uint8_t Rx_Enable_Bit	:1;
		uint8_t Tx_Enable_Bit	:1;
		uint8_t Rx_INT_Enable_Bit:1;
		uint8_t Tx_INT_Enable_Bit:1;
		
		uint8_t Stop_Bits:1;
		uint8_t	Data_Bits:2;
		uint8_t Check_Bits:2;
	}Uart_Flag;
	
	uint32_t BaudRate;
	
}Uart_Param_Type;

///function list //
EXTERN uint8_t MR88FX02_Uart_Init(CONST Uart_Table_Type* P,Uart_Param_Type *U0,Uart_Param_Type *U1,Uart_Param_Type *U2);
EXTERN void MR88FX02_Uart_Send(uint8_t Uart_xx, uint8_t Value);
EXTERN uint8_t MR88FX02_Uart_Receive(uint8_t Uart_xx);

EXTERN CONST Port_Table_Type Port_Table[];
EXTERN CONST Adc_Table_Type Adc_Table[];
EXTERN CONST Timer_PWM_Table_Type Timer_PWM_Table[];
EXTERN CONST Timer_Interval_Table_Type Timer_Interval_Table[];
EXTERN CONST Uart_Table_Type Uart_Table[];

#endif // end of _MR88FX02_INIT_H

